angular.module('starter.controllers', [])

.controller('AppCtrl', ['$scope','$rootScope', '$ionicModal', '$timeout','login',function($scope,$rootScope,$ionicModal, $timeout,login) {
    //登陆状态
    $rootScope.islogin=false;
    //登陆信息
    $scope.loginData = {
        username:"",
        password:""
    };
    //注册登陆弹窗
    $ionicModal.fromTemplateUrl('templates/login.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    //隐藏登陆框
    $rootScope.closeLogin = function() {
      $scope.modal.hide();
    };
    //显示登陆框
    $rootScope.login = function() {
      $scope.modal.show();
    };
    //判断是否登陆
    $rootScope.JudgeLogin=function(){
        login.isLog().success(function(data){
             console.log(data);
             if(data.success){
                  $rootScope.islogin=true;
                  $rootScope.closeLogin();
             }else{
                  $rootScope.islogin=false;
                  $rootScope.login();
             };
        });
    };
    //登陆
    $scope.doLogin = function(a) {
        if(a){
             login.log($scope.loginData.username,$scope.loginData.password).success(function(data){
                 if(data.success){
                      //登陆成功
                      $scope.closeLogin();
                      $rootScope.storeName=data.obj.storeName;
                      $rootScope.islogin=true;
                      window.location.reload();
                  }else{
                      //登陆失败
                     $rootScope.islogin=false;
                     alert(data.message);
                 }
             });
        }else{
            //账号密码格式错误
            alert("用户名或密码格式错误")
        }
    };
}])
.controller('recordCtrl',['$scope','$rootScope','login','StoreData',function($scope,$rootScope,login,StoreData){
      // $rootScope.JudgeLogin();
      // StoreData.get().success(function(data){
      //     $scope.recordList=data;
      // });
}])

.controller('submitStoreDataCtrl',['$scope','$rootScope','StoreData','login','$ionicBackdrop','$timeout',function($scope,$rootScope,StoreData,login,$ionicBackdrop, $timeout){
    // $rootScope.JudgeLogin();
    $('.form_date').datetimepicker({
            language:  'zh-CN',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0
    });
    $scope.storeData={
        storeName:$rootScope.storeName,
        subDate:"",
        tableSize:"",
        peopleSize:"",
        packSize:"",
        pct:"",
        money:"",
        monthMoney:""
    };
    $scope.submitData=function(a){
      var StoreDataStr='';
      var _Date=$('.js-subdate').val().split("-");   //<!----  太特么坑爹了,js写进去的value值,双向绑定不上
      var _D={
          year:"",
          month:"",
          day:"",
          text:""
      };
      var errorMsg="";
      if(_Date.length==1){
          errorMsg+="请选择日期\n"
      }
      if(!a){
          $('.js-errorMsg div:visible').each(function(){
             errorMsg+=$(this).html()+"\n"
          })
          alert(errorMsg);
          return
      };
      StoreDataStr+='<li class="item"><span class="c-list">桌数：</span><span>'+$scope.storeData.tableSize+'</span></li>';
      StoreDataStr+='<li class="item"><span class="c-list">人数：</span><span>'+$scope.storeData.peopleSize+'</span></li>';
      StoreDataStr+='<li class="item"><span class="c-list">打包：</span><span>'+$scope.storeData.packSize+'</span></li>';
      StoreDataStr+='<li class="item"><span class="c-list">客单价：</span><span>'+$scope.storeData.pct+'</span></li>';
      StoreDataStr+='<li class="item"><span class="c-list">营业额：</span><span>'+$scope.storeData.money+'</span></li>';
      StoreDataStr+='<li class="item"><span class="c-list">月营业额：</span><span>'+$scope.storeData.monthMoney+'</span></li>';
      _D.year=_Date[0];
      _D.month=_Date[1];
      _D.day=_Date[2];
      _D.text=StoreDataStr;
      StoreData.submit(_D).success(function(data){
          if(data.success){
            alert("提交成功！")
            window.location.reload();
          }else{
              if(data.message=="登陆超时"){
                  $rootScope.islogin=false;
              }
              alert(data.message);
          };
      });
    }
}])



